package com.bhs.sansonglogistics.ui.wallet;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.BankCardData;
import com.bhs.sansonglogistics.bean.BankCardList;
import com.bhs.sansonglogistics.dialog.MyClickListener2;

import com.bhs.sansonglogistics.net.NetCallBack;

import com.bhs.sansonglogistics.ui.login.RealNameAuthenticationActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 银行卡
 */
public class BankCardActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private RecyclerView mRvList;
    private BaseQuickAdapter<BankCardList, BaseViewHolder> mAdapter;
    private TextView mTvAddCard;
    private TextView mTvSet;
    private int status = 1;
    private int removeId = -1;

    @Override
    public int initLayout() {
        return R.layout.activity_bank_card;
    }

    @Override
    public void initView() {
        mRvList = findViewById(R.id.rv_list);
        mRvList.setVisibility(View.GONE);
        mTvAddCard = findViewById(R.id.tv_add_card);
        mTvAddCard.setOnClickListener(this);
        mTvSet = findViewById(R.id.tv_set);
        mTvSet.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<BankCardList, BaseViewHolder>(R.layout.item_bank_card) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, BankCardList item) {
                String bank_number = item.getBank_number();
                bank_number = bank_number.substring(bank_number.length() - 4);
                helper.setText(R.id.tv_card_number, String.format("**** **** **** %s", bank_number));
                helper.setText(R.id.tv_card_type, item.getBank_name());
                helper.setText(R.id.tv_type, MyUtils.getCNCardType(item.getBank_type()));
                helper.setGone(R.id.iv_checked, status == 2);
                helper.setImageResource(R.id.iv_checked, item.isChecked() ? R.mipmap.ic_checked : R.mipmap.ic_unchecked);
                helper.addOnClickListener(R.id.iv_checked);
            }
        };
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
        mRvList.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (removeId != position) {
                    if (removeId != -1) {
                        mAdapter.getData().get(removeId).setChecked(false);
                    }
                    removeId = position;
                    mAdapter.getData().get(position).setChecked(true);
                    mAdapter.notifyDataSetChanged();
                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        networkRequest(netApi.get_bank_list(20), this);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_set) {
            if (mTvSet.getText().toString().equals("管理")) {
                status = 2;
                mTvAddCard.setText("移除");
                mTvSet.setText("完成");
            } else {
                status = 1;
                mTvAddCard.setText("+增加银行卡");
                mTvSet.setText("管理");
            }
            mAdapter.notifyDataSetChanged();
        } else if (view.getId() == R.id.tv_add_card) {
            if (mTvAddCard.getText().toString().equals("移除")) {
                boolean isSelect = false;
                for (BankCardList datum : mAdapter.getData()) {
                    if (datum.isChecked()) {
                        isSelect = true;
                    }
                }
                if (isSelect) {
                    networkRequest(netApi.del_bank(mAdapter.getData().get(removeId).getBank_id()), this, 666);
                } else {
                    ToastUtil.show("请选择一张银行卡");
                }
            } else {
                voucher();
            }
        }
    }

    private void voucher() {
        boolean is_approve = SharedPreferencesUtils.getBoolean(mActivity, "is_approve", false);
        if (is_approve) {
            startActivity(new Intent(mActivity, AddCardActivity.class).putExtra("bank_type", 1));
        } else {
            new XPopup.Builder(mActivity)
                    .asConfirm("提示", "你还未实名认证，现在去认证",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @SuppressLint("NotifyDataSetChanged")
                                @Override
                                public void onConfirm() {
                                    startActivity(new Intent(mActivity, RealNameAuthenticationActivity.class));
                                }
                            }, null, false)
                    .show();
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            BankCardData data = new Gson().fromJson(json, BankCardData.class);
            if (data.isStatus()) {
                if (!MyUtils.isEmpty(data.getData().getList())) {
                    mRvList.setVisibility(View.VISIBLE);
                }
                mAdapter.setNewData(data.getData().getList());
            } else {
                ToastUtil.show(data.getMsg());
            }
        } else if (requestCode == 666) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                mAdapter.remove(removeId);
                removeId = -1;
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}